<template>
  <page-header-wrapper>
    <a-card :bordered="false">

      <a-table :columns="columns" :data-source="data">
        <span slot="action" slot-scope="text, record">
          <a>Invite 一 {{ record.name }}</a>
          <a-divider type="vertical"/>
          <a>Delete</a>
          <a-divider type="vertical"/>
          <a class="ant-dropdown-link"> More actions <a-icon type="down"/> </a>
        </span>
      </a-table>


      <!--      <s-table-->
      <!--        ref="table"-->
      <!--        size="default"-->
      <!--        rowKey="key"-->
      <!--        :columns="columns"-->
      <!--        :data="loadData"-->
      <!--        :alert="true"-->
      <!--        :rowSelection="rowSelection"-->
      <!--        showPagination="auto"-->
      <!--      >-->
      <!--        <span slot="serial" slot-scope="text, record, index">-->
      <!--          {{ index + 1 }}-->
      <!--        </span>-->
      <!--        <span slot="status" slot-scope="text">-->
      <!--          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />-->
      <!--        </span>-->
      <!--        <span slot="description" slot-scope="text">-->
      <!--          <ellipsis :length="4" tooltip>{{ text }}</ellipsis>-->
      <!--        </span>-->
      <!--      </s-table>-->

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
      <step-by-step-modal ref="modal" @ok="handleOk"/>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import {STable, Ellipsis} from '@/components'

import {getHistory} from "../../api/history";

import StepByStepModal from './modules/StepByStepModal'
import CreateForm from './modules/CreateForm'

const columns = [
  {
    title: '#',
    dataIndex: 'id',
    key: 'id',
    scopedSlots: {customRender: 'serial'}
  },
  {
    title: '被举报商家Id',
    dataIndex: 'merchant.id',
    scopedSlots: {customRender: 'status'}
  },
  {
    title: '店铺名',
    dataIndex: 'merchant.name',
    scopedSlots: {customRender: 'status'}
  },
  {
    title: '商家电话',
    dataIndex: 'merchant.phone',
    scopedSlots: {customRender: 'status'}
  },
  {
    title: '举报信息',
    dataIndex: 'info',
  },
  {
    title: '更新时间',
    dataIndex: 'updateAt',
    sorter: true
  }
]

const statusMap = {
  0: {
    status: 'default',
    text: '关闭'
  },
  1: {
    status: 'processing',
    text: '运行中'
  },
  2: {
    status: 'success',
    text: '已上线'
  },
  3: {
    status: 'error',
    text: '异常'
  }
}

export default {
  name: 'AdmissionTicket',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    StepByStepModal
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        phone: '',
        paymentType: '',
      },
      // 加载数据方法 必须为 Promise 对象
      selectedRowKeys: [],
      selectedRows: [],
      data: [],
      current: 1,
      total: 0,
      pages: 1,
      size: 100000
    }
  },
  filters: {
    statusFilter(type) {
      return statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type].status
    }
  },
  mounted() {
    this.getDate()
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = {...record}
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('修改成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub(record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    getDate() {
      let _this = this;
      getHistory({
        // current: _this.current,
        // size: _this.size,
        // type: 1,
        // 'user.phone': _this.queryParam.phone !== '' ? _this.queryParam.phone : null,
        // paymentType: _this.queryParam.paymentType !== '' ? _this.queryParam.paymentType : null
      }).then((res) => {
        _this.data = res.data;
        console.log(res)
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
